/*
 * Copyright 2016 Yan Zhenjie
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mingmen.canting.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.google.gson.Gson;
import com.mingmen.canting.R;
import com.mingmen.canting.adapter.CangKuAdapter;
import com.mingmen.canting.adapter.QueRenlingAdapter;
import com.mingmen.canting.http.callback.StringCallback;
import com.mingmen.canting.httpconfig.HttpResponse;
import com.mingmen.canting.model.CangKuModel;
import com.mingmen.canting.model.LoginBean;
import com.mingmen.canting.util.GsonUtil;
import com.mingmen.canting.util.PinnedHeaderDecoration;
import com.mingmen.canting.util.SharePreferenceUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;
import okhttp3.Call;
import okhttp3.Response;


/**
 * <p>
 * ListView形式的侧滑菜单，支持横向和竖向的。
 * </p>
 * Created by Yan Zhenjie on 2016/7/27.
 */
public class CangKuListActivity extends Activity {
    @BindView(R.id.img_return)
    ImageView imgReturn;

    @BindView(R.id.layout_close)
    LinearLayout layoutClose;
    private List<LoginBean.DataBean> mContactModels;
    private List<LoginBean.DataBean> mShowModels;
    private RecyclerView mRecyclerView;


    private CangKuAdapter mAdapter;

    String token;
    String applyId;

    List<CangKuModel.DataBean> cangKuModelList = new ArrayList<>();

    List<String> warehouseIdList = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cangkulingqu);

        ButterKnife.bind(this);
        token = (String) SharePreferenceUtil.get(this, "token", "");
        applyId = getIntent().getStringExtra("applyId");
        // RecyclerView设置相关
        mRecyclerView = (RecyclerView) findViewById(R.id.main_recycler);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new CangKuAdapter(this, cangKuModelList);

        final PinnedHeaderDecoration decoration = new PinnedHeaderDecoration();
        decoration.registerTypePinnedHeader(1, new PinnedHeaderDecoration.PinnedHeaderCreator() {
            @Override
            public boolean create(RecyclerView parent, int adapterPosition) {
                return true;
            }
        });
//        mRecyclerView.addItemDecoration(decoration);
        mRecyclerView.setAdapter(mAdapter);

        HttpResponse.getCangKuList(token, "1", new StringCallback(this) {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                Log.d("CangKuListActivityCangKuListActivity", s);
                CangKuModel cangKuModel = new CangKuModel();
                cangKuModel = GsonUtil.GsonToBean(s, CangKuModel.class);
//                cangKuModelList = cangKuModel.getData();

                cangKuModelList.addAll(cangKuModel.getData());
                mAdapter.setDataBeanList(cangKuModelList);
                for (int i = 0; i < cangKuModelList.size(); i++) {
                    warehouseIdList.add(cangKuModelList.get(i).getWarehouseId());
                }

            }
        });
        mAdapter.setOnitemClickLintener(new CangKuAdapter.OnitemClick() {
            @Override
            public void onItemClick(int position) {

//                Toast.makeText(CangKuListActivity.this, cangKuModelList.get(position).getWarehouseId() + "", Toast.LENGTH_SHORT).show();
//
                Intent intent = new Intent(CangKuListActivity.this, QueRenLingQuActivity.class);
                intent.putExtra("warehouseId", cangKuModelList.get(position).getWarehouseId());
                intent.putExtra("warehouse", cangKuModelList.get(position).getWarehouse());
                setResult(100, intent);
                finish();
            }
        });

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    @OnClick({R.id.img_return, R.id.layout_close})
    public void onViewClicked(View view) {
        switch (view.getId()) {

            case R.id.img_return:
            case R.id.layout_close:

                Intent intent = new Intent(CangKuListActivity.this, RegisterActivity.class);
                intent.putExtra("companyName", "");
                intent.putExtra("companyId", "");
                setResult(100, intent);
                finish();

                break;

        }
    }


}